package com.hsbc.dev.teamo4.sms.common.utils.data;

import com.google.common.collect.Lists;
import com.hsbc.dev.teamo4.sms.common.utils.factory.DBConnectFactory;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapHandler;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * @author:Kevin
 * @version:
 * @Project: permission_manage
 * @Package: com.hsbc.dev.teamo4.sms.common.utils.data
 * @Description:
 * @Date date: 2018/7/29
 */
public class DBUtil {
    private static QueryRunner queryRunner;

    /**
     * save
     * @param sql
     * @param object
     * @param ignoreList
     * @return
     */
    public static Object saveEntity(String sql,Object object,List ignoreList){
        queryRunner= DBConnectFactory.getQueryRunner();
        try {
            int row=queryRunner.update(sql, ReflectionUtils.getFields(object,ignoreList));
            if (row>0){
                return object;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * updateEntity
     * @param sql
     * @param object
     * @param ignoreList
     * @param id
     * @return
     */
    public static Object updateEntity(String sql,Object object,List ignoreList,String id){
        queryRunner= DBConnectFactory.getQueryRunner();
        Object[] params=ReflectionUtils.getFields(object, ignoreList);
        List<Object> list=Lists.newArrayList(params);
        list.add(id);
        try {
            int row = queryRunner.update(sql,list.toArray());
            if(row>0){
                return object;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * queryById
     * @param sql
     * @param beanHandler
     * @return
     */
    public static Object queryById(String sql, BeanHandler beanHandler){
        queryRunner= DBConnectFactory.getQueryRunner();
        try {
            return queryRunner.query(sql,beanHandler);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return  null;
    }

    public static Object queryAllEntity(String sql, BeanListHandler beanListHandler){
        queryRunner= DBConnectFactory.getQueryRunner();
        try {
            return queryRunner.query(sql,beanListHandler);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Object queryString(String sql, ArrayHandler arrayHandler){
        queryRunner= DBConnectFactory.getQueryRunner();
        try {
            return queryRunner.query(sql,arrayHandler);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static int updateByIsEnable(String sql,String id,Boolean bool){
        queryRunner= DBConnectFactory.getQueryRunner();
        try {
            int row=queryRunner.update(sql, bool,id);
            if(row>0){
                return row;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static Object countAndToMap(String sql, MapHandler mapHandler){
        queryRunner= DBConnectFactory.getQueryRunner();
        try {
            return queryRunner.query(sql,mapHandler);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return  null;
    }
}
